package com.fuerda.izb.test

import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.ViewAnimator
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.animation.addListener
import com.fuerda.izb.R
import com.fuerda.izb.base.IBaseActivity
import com.fuerda.izb.databinding.LayoutView1Binding
import com.fuerda.izb.utils.ToastUtil
import com.fuerda.izb.utils.dp2px


class TestAct : IBaseActivity<LayoutView1Binding>() {

    override fun initView(savedInstanceState: Bundle?) {
        with(mBinding) {
            animateWidthChange(mainLay1, speedMark)
            animateWidthChange(mainLay2, speedMark2)
            speedMark.setTexts(
                this@TestAct.resources.getStringArray(R.array.speed_array).toMutableList()
            )

            speedMark2.setTexts(
                this@TestAct.resources.getStringArray(R.array.temperature_array).toMutableList()
            )
        }
        addSpeedBarListener()

        mBinding.mv1.setOnLongClickListener {
            //返回true，表示事件已经被处理不需要继续传递给其他监听器。返回false，则事件会继续传递
            true
        }
    }

    override fun initData() {
        with(mBinding) {
            leftBtn.setOnClickListener {
                ToastUtil.showToast("减")
            }
            rightBtn.setOnClickListener {
                ToastUtil.showToast("加")
            }
        }
    }

    private fun animateWidthChange(view1: View, view2: View) {
        val startWidth = dp2px(this, 90) //两倍bar_high
        val endWidth = dp2px(this, 330)

        ValueAnimator.ofInt(startWidth, endWidth).apply {
            duration = 1000 // 动画持续时间
            addUpdateListener {
                val newWidth = it.animatedValue as Int
                val params = view1.layoutParams as ViewGroup.LayoutParams
                params.width = newWidth
                view1.requestLayout() // 请求重新布局以应用新的宽度
            }
            start() // 启动动画
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    animateTopChange(view2)
                }
            })
        }

    }

    private fun animateTopChange(view: View) {
        val start = dp2px(this, 0)
        val end = dp2px(this, 42)  //bar_high的值

        ValueAnimator.ofInt(start, end).apply {
            duration = 1000 // 动画持续时间
            addUpdateListener {
                val newWidth = it.animatedValue as Int
                val params = view.layoutParams as ConstraintLayout.LayoutParams
                params.topMargin = newWidth
                view.requestLayout() // 请求重新布局以应用新的宽度
            }
            start() // 启动动画
        }
    }

    private fun addSpeedBarListener() {
        mBinding.speedBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                mBinding.percentView.apply {
                    setColorBlockPercentage(progress)
                }
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }
        })

        mBinding.speedBar2.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {

            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }
        })
    }
}

